// IndexedDB数据查看工具

// 查看所有支付记录
export async function viewAllPaymentRecords(): Promise<void> {
    try {
        const dbName = 'PaymentRecordsDB'
        const request = indexedDB.open(dbName, 2)

        request.onsuccess = () => {
            const db = request.result
            const transaction = db.transaction(['payment_records'], 'readonly')
            const store = transaction.objectStore('payment_records')
            const getAllRequest = store.getAll()

            getAllRequest.onsuccess = () => {
                const records = getAllRequest.result
                console.log('=== 所有支付记录 ===')
                console.table(records)
                console.log(`总计: ${records.length} 条记录`)
            }
        }
    } catch (error) {
        console.error('查看支付记录失败:', error)
    }
}

// 查看所有费率设置
export async function viewAllRateSettings(): Promise<void> {
    try {
        const dbName = 'PaymentRecordsDB'
        const request = indexedDB.open(dbName, 2)

        request.onsuccess = () => {
            const db = request.result
            const transaction = db.transaction(['rate_settings'], 'readonly')
            const store = transaction.objectStore('rate_settings')
            const getAllRequest = store.getAll()

            getAllRequest.onsuccess = () => {
                const settings = getAllRequest.result
                console.log('=== 所有费率设置 ===')
                console.table(settings)
                console.log(`总计: ${settings.length} 条记录`)
            }
        }
    } catch (error) {
        console.error('查看费率设置失败:', error)
    }
}

// 查看特定会员的支付记录
export async function viewMemberPaymentRecords(memberId: string): Promise<void> {
    try {
        const dbName = 'PaymentRecordsDB'
        const request = indexedDB.open(dbName, 2)

        request.onsuccess = () => {
            const db = request.result
            const transaction = db.transaction(['payment_records'], 'readonly')
            const store = transaction.objectStore('payment_records')
            const index = store.index('member_id')
            const getRequest = index.getAll(memberId)

            getRequest.onsuccess = () => {
                const records = getRequest.result
                console.log(`=== 会员 ${memberId} 的支付记录 ===`)
                console.table(records)
                console.log(`总计: ${records.length} 条记录`)
            }
        }
    } catch (error) {
        console.error('查看会员支付记录失败:', error)
    }
}

// 查看特定会员的费率设置
export async function viewMemberRateSettings(memberId: string): Promise<void> {
    try {
        const dbName = 'PaymentRecordsDB'
        const request = indexedDB.open(dbName, 2)

        request.onsuccess = () => {
            const db = request.result
            const transaction = db.transaction(['rate_settings'], 'readonly')
            const store = transaction.objectStore('rate_settings')
            const index = store.index('member_id')
            const getRequest = index.get(memberId)

            getRequest.onsuccess = () => {
                const settings = getRequest.result
                console.log(`=== 会员 ${memberId} 的费率设置 ===`)
                if (settings) {
                    console.log(settings)
                } else {
                    console.log('没有找到费率设置')
                }
            }
        }
    } catch (error) {
        console.error('查看会员费率设置失败:', error)
    }
}

// 清空所有数据（谨慎使用）
export async function clearAllData(): Promise<void> {
    try {
        const dbName = 'PaymentRecordsDB'
        const request = indexedDB.deleteDatabase(dbName)

        request.onsuccess = () => {
            console.log('数据库已清空')
        }

        request.onerror = () => {
            console.error('清空数据库失败')
        }
    } catch (error) {
        console.error('清空数据失败:', error)
    }
}

// 导出所有数据查看函数到全局
export function setupDBViewer() {
    // 将函数添加到全局window对象，方便在控制台调用
    (window as any).viewAllPaymentRecords = viewAllPaymentRecords
        ; (window as any).viewAllRateSettings = viewAllRateSettings
        ; (window as any).viewMemberPaymentRecords = viewMemberPaymentRecords
        ; (window as any).viewMemberRateSettings = viewMemberRateSettings
        ; (window as any).clearAllData = clearAllData

    console.log('=== IndexedDB查看工具已加载 ===')
    console.log('可用命令:')
    console.log('- viewAllPaymentRecords() - 查看所有支付记录')
    console.log('- viewAllRateSettings() - 查看所有费率设置')
    console.log('- viewMemberPaymentRecords("会员ID") - 查看特定会员的支付记录')
    console.log('- viewMemberRateSettings("会员ID") - 查看特定会员的费率设置')
    console.log('- clearAllData() - 清空所有数据（谨慎使用）')
} 